package com.cplatform.movie.back.avtivities.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cplatform.dbhelp.DbHelper;
import com.cplatform.dbhelp.page.Page;
import com.cplatform.movie.back.avtivities.dao.ActivitiesDao;
import com.cplatform.movie.back.avtivities.entity.Activities;
import com.cplatform.movie.back.utils.TimeUtil;

/**
 * Title.活动业务层 <br>
 * Description.
 * <p>
 * Copyright: Copyright (c) 2015年6月28日 下午12:59:07
 * <p>
 * Author: liuyt@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
@Service
public class ActivitiesService {
	@Autowired
	private ActivitiesDao activitiesDao;
	@Autowired
	private DbHelper dbHelper;

	/**
	 * @Title: 获取活动分页数据 
	 * <br>
	 * @param ac
	 * @param page
	 * @return
	 * @throws SQLException    
	 * @throws
	 */
	public Page<Activities> list(Activities ac, int page) throws SQLException {
		StringBuilder sql = new StringBuilder();
		List<Object> params = new ArrayList<Object>();
		sql.append("select * from t_small_activieits t where t.status!=-1");

		if (ac.getType() != null) {
			sql.append(" and t.type=?");
			params.add(ac.getType());
		}

		if (StringUtils.isNotBlank(ac.getName())) {
			sql.append(" and t.name=?");
			params.add(ac.getName());
		}

		if (StringUtils.isNotBlank(ac.getCreateTime())) {
			sql.append(" and t.create_time>=?");
			params.add(ac.getCreateTime() + "000000");
		}

		if (StringUtils.isNotBlank(ac.getCreateEndTime())) {
			sql.append(" and t.create_time<=?");
			params.add(ac.getCreateEndTime() + "235959");
		}
		
		if (StringUtils.isNotBlank(ac.getStartTime())) {
			sql.append(" and t.START_TIME>=?");
			params.add(ac.getStartTime() + "000000");
		}
		
		if (StringUtils.isNotBlank(ac.getEndTime())) {
			sql.append(" and t.end_time<=?");
			params.add(ac.getEndTime() + "235959");
		}
		
		sql.append(" order by t.id desc");
		return dbHelper.getPage(sql.toString(), Activities.class, page, Page.DEFAULT_PAGESIZE, params.toArray());
	}

	/**
	 * @Title: 活动新增 
	 * <br>
	 * @param ac
	 * @param id
	 * @return    
	 * @throws
	 */
	public Activities save(Activities ac, Long userId) {
		ac.setCreateTime(TimeUtil.now(TimeUtil.SOURCE_1));
		ac.setCreateUser(userId);
		ac.setStatus(0l);
		ac.setIsValid(0l);
		return activitiesDao.save(ac);
	}

	/**
	 * @Title: 活动更新 
	 * <br>
	 * @param ac
	 * @param activities    
	 * @throws
	 */
	public void updete(Activities ac, Activities activities) {
		activities.setExplain(ac.getExplain());
		activities.setStartTime(ac.getStartTime());
		activities.setEndTime(ac.getEndTime());
		activities.setOrderItemNum(ac.getOrderItemNum());
		activities.setFreeItemNum(ac.getFreeItemNum());
		activities.setOrderTotalPrice(ac.getOrderTotalPrice());
		activities.setFreeTotalPrice(ac.getFreeTotalPrice());
		activities.setImgPath(ac.getImgPath());
		activities.setName(ac.getName());
		activities.setType(ac.getType());
		activities.setStatus(0l);
		activities.setIsValid(0l);
		activitiesDao.save(activities);
	}

	/**
	 * @throws SQLException 
	 * @Title: 商品关联活动弹框 
	 * <br>
	 * @param ac
	 * @param page
	 * @return    
	 * @throws
	 */
	public Page<Activities> listSelect(Activities ac, int page) throws SQLException {
		StringBuilder sql = new StringBuilder();
		List<Object> params = new ArrayList<Object>();
		sql.append("select * from t_small_activieits t where t.status!=-1 and t.is_valid=1");

		if (ac.getType() != null) {
			sql.append(" and t.type=?");
			params.add(ac.getType());
		}

		if (StringUtils.isNotBlank(ac.getName())) {
			sql.append(" and t.name=?");
			params.add(ac.getName());
		}

		if (StringUtils.isNotBlank(ac.getCreateTime())) {
			sql.append(" and t.create_time>=?");
			params.add(ac.getCreateTime() + "000000");
		}

		if (StringUtils.isNotBlank(ac.getCreateEndTime())) {
			sql.append(" and t.create_time<=?");
			params.add(ac.getCreateEndTime() + "235959");
		}
		
		if (StringUtils.isNotBlank(ac.getStartTime())) {
			sql.append(" and t.START_TIME>=?");
			params.add(ac.getStartTime() + "000000");
		}
		
		if (StringUtils.isNotBlank(ac.getEndTime())) {
			sql.append(" and t.end_time<=?");
			params.add(ac.getEndTime() + "235959");
		}
		
		sql.append(" order by t.id desc");
		
		return dbHelper.getPage(sql.toString(), Activities.class, page, Page.DEFAULT_PAGESIZE, params.toArray());
	}

}
